package com.cq.hd.app.biz;

import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.QrCodeCreateUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.order.api.TbOrderItemApi;
import com.cq.hd.order.api.vo.OrderItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Base64;

@Component
@Slf4j
public class QrCodeBiz implements Serializable {

    @Autowired
    private TbOrderItemApi orderItemApi;

    public String getQrCode(String verifyCode) {
        if (StringUtils.isBlank(verifyCode)) {
            Throw.isBusinessException("核销码不能为空");
        }

//        OrderItemVo orderItemVo = orderItemApi.getByVerifyCode(verifyCode).unpack();
//        if (orderItemVo == null) {
//            Throw.isBusinessException("未找到订单数据");
//        }

        try {
            byte[] qrCode = QrCodeCreateUtil.createQrCode(verifyCode, 900);
            Base64.Encoder encoder = Base64.getEncoder();
            return "data:image/jpeg;base64," + encoder.encodeToString(qrCode);
        } catch (Exception e) {
            log.error("生成核销码失败，verifyCode：{}", verifyCode);
            Throw.isBusinessException("获取核销码失败");
        }

        return "";
    }

}
